package cn.shierblog.spider_zhiping;

import cn.hutool.core.util.NumberUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.MessageFormat;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

public class ZhilianSpider extends AbstractSpider{

    static LinkedBlockingQueue<Job> jobs = new LinkedBlockingQueue<>();

    static String url = "https://m.zhaopin.com/sou/{0}--?keyword=%E4%BE%9B%E5%BA%94%E9%93%BE&provinceCode={1}&city={2}";

    public ZhilianSpider(String url) {
        super(url);
    }

    public static void main(String[] args) {

        System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");

        new ZhilianSpider(MessageFormat.format(url, 532,532,532)).craw();

    }

    static void getNext(ChromeDriver driver, int pageNow) {
        //
        if (pageNow>3) {
            return;
        }
        String pageSource = driver.getPageSource();
        String path = (ZhilianSpider.class.getResource("/").getPath() + (pageNow) + "t.html").substring(1);
        File file = new File(path);
        if (!Files.exists(Paths.get(path))) {
            try {
                Files.createFile(Paths.get(path));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
            fileOutputStream.write(pageSource.getBytes());
            Document parse = Jsoup.parse(pageSource);
            Element element = parse.getElementsByTag("script").get(9);
            String data = element.data();
            data = data.replace("__INITIAL_STATE__=", "");
            JSONObject jsonObject = JSONObject.parseObject(data);
            JSONObject positionList = jsonObject.getJSONObject("positionList");
            JSONArray list = positionList.getJSONArray("list");
            if (list.isEmpty()) {
                return;
            }
            list.forEach(c->{
                JSONObject ss = (JSONObject)c;
                String cityDistrict = ss.getString("cityDistrict");

                String companyName = ss.getString("companyName");
                String education = ss.getString("education");
                String emplType = ss.getString("emplType");
                String name = ss.getString("name");
                String salary = ss.getString("salary");
                Job job = new Job();
                job.setName(companyName);
                job.setJob(name);
                job.setCity(cityDistrict);
                job.setEdu(education);
                job.setSalary(salary);
                job.setType(emplType);
                System.out.println(job.toString());
                jobs.add(job);
            });
            WebElement next = driver.findElement(By.className("van-pagination__next"));
            if (next != null) {
                driver.executeScript("arguments[0].click();", next);
                getNext(driver, pageNow + 1);
            }
        } catch (Exception e) {

        }


    }

    @Override
    public void spider() throws Exception {
        String curUrl = url;
        for (int i = 532; i < 800; i++) {
            //726
            int i1 = i+1;
            curUrl = MessageFormat.format(url, i1,i1,i1).replace(",","");
            driver.get(curUrl);
            TimeUnit.SECONDS.sleep(3);
            getNext(driver, 1);
        }
    }

    static class Job {
        private String job;
        private String name;
        private String salary;
        private String city;
        private String edu;
        private String type;

        @Override
        public String toString() {
            return "{" +
                    "job='" + job + '\'' +
                    ", name='" + name + '\'' +
                    ", salary='" + salary + '\'' +
                    ", city='" + city + '\'' +
                    ", edu='" + edu + '\'' +
                    ", type='" + type + '\'' +
                    '}';
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getJob() {
            return job;
        }

        public void setJob(String job) {
            this.job = job;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getSalary() {
            return salary;
        }

        public void setSalary(String salary) {
            this.salary = salary;
        }

        public String getCity() {
            return city;
        }

        public void setCity(String city) {
            this.city = city;
        }

        public String getEdu() {
            return edu;
        }

        public void setEdu(String edu) {
            this.edu = edu;
        }
    }
}
